package com.mouse.view;

import com.mouse.commen.Message;
import com.mouse.commen.MessageType;
import com.mouse.po.User;

import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

public class RegisterView extends JFrame {
    public static void main(String[] args) {
        RegisterView registerView = new RegisterView();
        registerView.createFrame();
    }

    //注册界面中部的所有组件
    JPanel centerJPanel = null;
    JLabel userNameJLabel = null;
    JTextField userNameJTextField = null;
    JLabel pwdJLabel = null;
    JPasswordField pwdJPasswordField = null;
    JLabel realNameJLabel = null;
    JTextField realNameJTextField = null;

    //南部组件包括提交按钮和重置按钮
    JPanel southJPanel = null;
    JButton submitJButton = null;
    JButton resetJButton = null;

    public void createFrame(){
        //中部组件的初始化
        centerJPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        userNameJLabel = new JLabel("用户账号：");
        userNameJTextField = new JTextField(24);
        userNameJTextField.setFont(new Font(Font.DIALOG,Font.PLAIN,16));
        pwdJLabel = new JLabel("用户密码：");
        pwdJPasswordField = new JPasswordField(24);
        pwdJPasswordField.setFont(new Font(Font.DIALOG,Font.PLAIN,16));
        realNameJLabel = new JLabel("用户姓名：");
        realNameJTextField = new JTextField(24);
        realNameJTextField.setFont(new Font(Font.DIALOG,Font.PLAIN,16));

        //装载中部组件
        centerJPanel.add(userNameJLabel);
        centerJPanel.add(userNameJTextField);
        centerJPanel.add(pwdJLabel);
        centerJPanel.add(pwdJPasswordField);
        centerJPanel.add(realNameJLabel);
        centerJPanel.add(realNameJTextField);
        this.add(centerJPanel,BorderLayout.CENTER);

        //南部组件初始化
        southJPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        //提交按钮点击事件
        submitJButton = new JButton("提交");
        submitJButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String pwd = new String(pwdJPasswordField.getPassword());

                //获取注册用户的信息
                User user = new User();
                user.setUsername(userNameJTextField.getText());
                user.setPwd(pwd);
                user.setRealname(realNameJTextField.getText());

                //装载好了客户端与服务器的通讯信息
                Message message = new Message();
                message.setMessageType(MessageType.REGISTER);
                message.setUser(user);

                try{
                    //和本机建立通信联系
                    Socket socket = new Socket("localhost", 8881);

                    //向服务端写注册信息
                    OutputStream outputStream = socket.getOutputStream();
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(message);
                    //从服务端读注册是否完成
                    InputStream inputStream = socket.getInputStream();
                    ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                    Message acceptMessage = (Message) objectInputStream.readObject();

                    if(acceptMessage.getMessageType()==MessageType.REGISTER_SUCCESS){
                        JOptionPane.showMessageDialog(RegisterView.this, acceptMessage.getContent(), "提示", JOptionPane.INFORMATION_MESSAGE);
                        RegisterView.this.dispose();
                    }else{
                        JOptionPane.showMessageDialog(RegisterView.this, acceptMessage.getContent(), "提示", JOptionPane.INFORMATION_MESSAGE);
                    }


                }catch (Exception exception){
                    exception.printStackTrace();
                }



            }
        });

        //重置按钮的点击事件
        resetJButton = new JButton("重置");
        resetJButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                userNameJTextField.setText("");
                pwdJPasswordField.setText("");
                realNameJTextField.setText("");
            }
        });
        //装载南部的组件
        southJPanel.add(submitJButton);
        southJPanel.add(resetJButton);
        this.add(southJPanel,BorderLayout.SOUTH);

        this.setTitle("QQ注册");
        this.setBounds(705,300,450,350);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
